#include "Blue.h"
#include "Source.h"
#define TimeJump 0.55f

Blue::Blue(LPDIRECT3DDEVICE9 d3ddv)
{
	//m_d3ddv = d3ddv;
	m_Position.y = GROUND_Y;
	m_Position.x = 10;
	m_Position.z = 0;

	m_Veclocity_vy = 0;
	m_Veclocity_vx = 0;

	m_Move = MoveImage::STAND;
	m_Effect = SpriteEffect::None;
	m_Sprite = Source::GetInstance()->GetSpriteBlue(m_Move);

	m_TimeAnimation = 0.12f;
	m_TimeShoot = 2 * m_TimeAnimation;// thoi gian load hinh ban

	m_TimeJump = 0;
	m_ToolBullet = new Toolbullet();
	m_Vpx = 0;
	m_gameTime = new GameTime();

	m_isAttack = false;
}

void Blue::loadPositionShootAtSprite(char *Path)
{
	ifstream file;
	file.open(Path);

	while (!file.eof())
	{
		int id;
		int direct;
		int x;
		int y;

		file >> id >> direct >> x >> y;

		ObjectTittle temp(id, direct, x, y);
		m_positionShootAtSprite.push_back(temp);
	}

	file.close();
}

void Blue::Update()
{
	if (m_gameTime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		m_Position.x += m_Veclocity_vx;
		m_Position.y += m_Veclocity_vy;

		m_TimeJump -= m_gameTime->getElapsedTimePerSecond();

		if (m_TypeMove == TypeMove::Jump)
		{
			m_Veclocity_vx *= 3.5f;
			m_Sprite = Source::GetInstance()->GetSpriteBlue(MoveImage::JUMP);
			if (m_TimeJump <= 0)
			{
				m_Position.y -= FALLDOWN_VELOCITY_DECREASE;

				m_Veclocity_vy = 0;
				m_TimeJump = 0;
			}

			m_Sprite->Next();
		}
		else
		{
			m_Sprite = Source::GetInstance()->GetSpriteBlue(m_Move);
		}

		if (m_Position.y - m_Sprite->GetHeight() / 2 < GROUND_Y)
		{
			m_Position.y = GROUND_Y;
			m_TypeMove = TypeMove::Move_OnTheLand;
		}

		// voi cac thao tac dung yen, huong len, nam xuong chi load sprite ke tiep khi co ban dan
		if (m_Move == MoveImage::UP || m_Move == MoveImage::STAND || m_Move == MoveImage::DOWN)
		{
			if (m_TimeShoot > 0)
			{
				m_TimeShoot -= m_TimeAnimation;
				m_Sprite->Next();
			}
		}
		else
		{
			m_TimeShoot = 0;
			m_Sprite->Next();
		}

		m_gameTime->Update();

		if (m_isAttack)
			UpdateShoot();
	}

	m_ToolBullet->Update();
}

void Blue::UpdateShoot()
{
	for (int i = 0; i < m_positionShootAtSprite.size(); i++)
	{
		// get position of the gun at every action of sprite
		if (m_positionShootAtSprite[i].m_Id == ID_POSITION_GUN_SPRITE_DOWN && m_Move == MoveImage::DOWN)
		{
			if (m_shootDirection == m_positionShootAtSprite[i].m_Index)
			{
				m_ToolBullet->InitBullet(m_Position.x, m_Position.y, m_positionShootAtSprite[i].m_X / 2, m_positionShootAtSprite[i].m_Y, m_Effect, m_shootDirection, m_TypeMove);
				m_ToolBullet->Shoot();
				break;
			}
		}
		else if (m_positionShootAtSprite[i].m_Id == ID_POSITION_GUN_ONTHELAND && m_Move != MoveImage::DOWN)
		{
			if (m_positionShootAtSprite[i].m_Index == m_shootDirection)
			{
				m_ToolBullet->InitBullet(m_Position.x, m_Position.y, m_positionShootAtSprite[i].m_X / 2, m_positionShootAtSprite[i].m_Y, m_Effect, m_shootDirection, m_TypeMove);
				m_ToolBullet->Shoot();
				break;
			}
		}
		else if (m_TypeMove == TypeMove::Jump)
		{
			m_ToolBullet->InitBullet(m_Position.x, m_Position.y, m_Sprite->getWidth() / 2, m_Sprite->GetHeight() / 2, m_Effect, m_shootDirection, m_TypeMove);
			m_ToolBullet->Shoot();
			break;
		}
	}

	m_isAttack = false;
}

void Blue::InputKey(CKeyBoard keyboard)
{
	m_gameTime->setTime();
	
	if (m_gameTime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		keyboard.GetState();
		m_Veclocity_vx = SPEED;

		if (keyboard.IsKeyDown(DIK_RIGHT) && keyboard.IsKeyDown(DIK_UP))
		{
			m_Effect = SpriteEffect::None;
			m_shootDirection = ShootDerection::_45_SHOOT;
			
			m_Move = MoveImage::_45_DEGREE;
		}
		else if (keyboard.IsKeyDown(DIK_RIGHT) && keyboard.IsKeyDown(DIK_DOWN) )
		{
			m_Effect = SpriteEffect::None;
			m_shootDirection = ShootDerection::_315_SHOOT;
						
			m_Move = MoveImage::_135_DEGREE;
		}
		else if (keyboard.IsKeyDown(DIK_LEFT) && keyboard.IsKeyDown(DIK_DOWN) )
		{
			m_Veclocity_vx = -SPEED;
			m_Effect = SpriteEffect::Flip;
			m_shootDirection = ShootDerection::_315_SHOOT;
						
			m_Move = MoveImage::_135_DEGREE;
		}
		else if (keyboard.IsKeyDown(DIK_LEFT) && keyboard.IsKeyDown(DIK_UP))
		{
			m_Veclocity_vx = -SPEED;
			m_Effect = SpriteEffect::Flip;
			m_shootDirection = ShootDerection::_45_SHOOT;

			m_Move = MoveImage::_45_DEGREE;
		}
		else if (keyboard.IsKeyDown(DIK_LEFT))
		{
			m_Veclocity_vx = -SPEED;
			m_Move = MoveImage::RIGHT_LEFT;
			m_Effect = SpriteEffect::Flip;
			
			m_shootDirection = ShootDerection::_0_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_RIGHT))
		{
			m_Move = MoveImage::RIGHT_LEFT;
			m_Effect = SpriteEffect::None;
			
			m_shootDirection = ShootDerection::_0_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_UP))
		{
			m_Veclocity_vx = 0;
			m_Move = MoveImage::UP;
			m_shootDirection = ShootDerection::_90_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_DOWN))
		{
			m_Veclocity_vx = 0;
			m_Move = MoveImage::DOWN;

			if (m_TypeMove == TypeMove::Move_OnTheLand)
				m_shootDirection = ShootDerection::_0_SHOOT;
			else if (m_TypeMove == TypeMove::Jump)
				m_shootDirection = ShootDerection::_270_SHOOT;
		}
		else
		{
			m_Veclocity_vx = 0;
			m_Move = MoveImage::STAND;
			m_shootDirection = ShootDerection::_0_SHOOT;
		}

		if (keyboard.IsPressed(DIK_SPACE) && m_Position.y <= GROUND_Y)
		{
			m_TimeJump = 4 * m_TimeAnimation;
			m_Veclocity_vy += VECLOCITY_VY;
			m_Move = MoveImage::JUMP;
			m_TypeMove = TypeMove::Jump;
		}

		// phim ban chinh
		if (keyboard.IsPressed(DIK_Z))
		{
			// chuyen doi image di chuyen-> image di chuyen + ban
			if (m_Move == MoveImage::RIGHT_LEFT && m_TypeMove != TypeMove::Jump)
				m_Move = MoveImage::RUN_SHOOT;
			
			m_TimeShoot = 2 * m_TimeAnimation;
			m_isAttack = true;
			m_ToolBullet->setGun(TypeOfGun::SpreadGun);
		}

		//them check dan mechine gun 
		if (keyboard.IsKeyDown(DIK_X))
		{
			if(m_Move == MoveImage::RIGHT_LEFT && m_TypeMove != TypeMove::Jump)
				m_Move = MoveImage::RUN_SHOOT;

			m_TimeShoot = 2 * m_TimeAnimation;
			m_isAttack = true;
			m_ToolBullet->setGun(TypeOfGun::MachineGun);
		}
	}
}

D3DXVECTOR3 Blue::GetPosition()
{
	return m_Position;
}

void Blue::Render(LPD3DXSPRITE SpriteHandler)
{
	if (m_Veclocity_vx > 0)
	{
		if ( m_Vpx <= m_Position.x - SCREEN_WIDTH / 2)
			m_Vpx = m_Position.x - SCREEN_WIDTH / 2;
	}
	if (m_Vpx <= 0) m_Vpx = 0;

	D3DXVECTOR2 scale;

	if (m_Effect == SpriteEffect::None)
	{
		scale.x = scale.y = 1.0f;
	}
	else
	{
		scale.x = -1.0f;
		scale.y = 1.0f;
	}

	m_Sprite->Draw(SpriteHandler, D3DXVECTOR3(m_Position.x, m_Position.y + m_Sprite->GetHeight() / 2, 0), scale, m_Vpx, VIEW_PORT_Y);

	m_ToolBullet->Draw(SpriteHandler, m_Vpx);
}

Blue::~Blue()
{
	/*if (m_d3ddv != NULL)
		m_d3ddv->Release();
*/
	delete m_gameTime;
}
